package org.lee.pdf.model;

import lombok.Data;
import lombok.Getter;

/**
 * @author jackielee
 * @version 1.0
 * @description ApiResult
 * @date 2021/5/6
 **/
@Data
public class ApiResult {

    private int code;
    private String message;

    private ApiResult(){}

    private ApiResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    @Getter
    private static class ApiResultInner<T> extends ApiResult{
        private T data;

        public ApiResultInner(int code, String message, T data) {
            super(code, message);
            this.data = data;
        }
    }

    public <T> T getData(){
        ApiResultInner<T> inner = (ApiResultInner<T>)this;
        return inner.getData();
    }

    public static ApiResult success(){
        return new ApiResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> ApiResult success(T data){
        return new ApiResultInner<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static ApiResult failure(ResultCode code, String message){
        return new ApiResult(code.getCode(), message.isEmpty() ? code.getMessage() : message);
    }
}
